#include "oglwindow.h"
#include "ui_oglwindow.h"

#include <QColor>
#include <QColorDialog>

extern unsigned int SCR_WIDTH;
extern unsigned int SCR_HEIGHT;

extern struct move_type MT;
extern struct texture_state TS;
extern struct animation_state AS;
extern struct draw_type DT;
extern struct light_type LT;

OGLWindow::OGLWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::OGLWindow)
{
    ui->setupUi(this);

    // move disable
    MT.move_type = 0;
    MT.xoffset = 0.0f;
    MT.yoffset = 0.0f;
    MT.r_xoffset = 0.0f;
    MT.r_yoffset = 0.0f;
    MT.lastX = SCR_WIDTH / 2.0f;
    MT.lastY = SCR_HEIGHT / 2.0f;
    MT.firstMouse = true;

    // animation disable
    AS.isAnimating = 0;

    // texture disable
    TS.texEnable = 0;
    TS.orgColor.setX(1.0f);
    TS.orgColor.setY(1.0f);
    TS.orgColor.setZ(1.0f);

    // default to draw faces
    DT.type = 3;

    // default to fixed point light
    LT.l_type = 1;
}

OGLWindow::~OGLWindow()
{
    delete ui;
}

void OGLWindow::keyPressEvent(QKeyEvent *event)
{
    ui->openGLWidget->keyPressEvent(event);
}

void OGLWindow::on_actionfps_camera_triggered()
{
    // fps camera
    MT.move_type = 1;
    ui->openGLWidget->setCursor(Qt::CrossCursor);   //改变鼠标

    ui->openGLWidget->initFpsCamera();
}

void OGLWindow::on_actionrotation_triggered()
{
    // rotate the object
    MT.move_type = 2;
    ui->openGLWidget->setCursor(Qt::UpArrowCursor);   //上箭头鼠标
}

void OGLWindow::on_actionmove_disable_triggered()
{
    // move disable
    MT.move_type = 0;
    ui->openGLWidget->setCursor(Qt::ArrowCursor);  //一般鼠标
}

void OGLWindow::on_actionfixed_point_light_triggered()
{
    // fixed point light
    LT.l_type = 1;
    ui->openGLWidget->update();
}

void OGLWindow::on_actiondirectional_light_triggered()
{
    // directional light
    LT.l_type = 2;
    ui->openGLWidget->update();
}

void OGLWindow::on_actionspot_light_triggered()
{
    // spot light
    LT.l_type = 3;
    ui->openGLWidget->update();
}

void OGLWindow::on_actionno_light_triggered()
{
    // lamp disable
    LT.l_type = 0;
    ui->openGLWidget->update();
}

void OGLWindow::on_actionboxcube_off_triggered()
{
    ui->openGLWidget->loadModelClarkBoxcube();

    ui->openGLWidget->update();
}

void OGLWindow::on_actioncube_off_triggered()
{
    ui->openGLWidget->loadModelClarkCube();

    ui->openGLWidget->update();
}

void OGLWindow::on_actionz_off_triggered()
{
    ui->openGLWidget->loadModeLoopZ();

    ui->openGLWidget->update();
}

void OGLWindow::on_actionclon_triggered()
{
    ui->openGLWidget->loadModelLoopCone();

    ui->openGLWidget->update();
}

void OGLWindow::on_actioncolor_triggered()
{
    QColor color = QColorDialog::getColor(Qt::black);
    if (color.isValid()) {
        TS.orgColor.setX(color.redF());
        TS.orgColor.setY(color.greenF());
        TS.orgColor.setZ(color.blueF());
    }
    ui->openGLWidget->update();
}

void OGLWindow::on_actionsubdivision_triggered()
{
    //
    ui->openGLWidget->subdivision();
    ui->openGLWidget->update();
}

void OGLWindow::on_actionpoint_triggered()
{
    // draw points
    DT.type = 1;
    ui->openGLWidget->update();
}

void OGLWindow::on_actionline_triggered()
{
    // draw lines
    DT.type = 2;
    ui->openGLWidget->update();
}

void OGLWindow::on_actionfill_triggered()
{
    // draw faces
    DT.type = 3;
    ui->openGLWidget->update();
}

void OGLWindow::on_actionoriginal_triggered()
{
    ui->openGLWidget->original();
    ui->openGLWidget->update();
}
